// Create javadocs with:
// javadoc -author -private -d docs TimeCard.java PayrollApp.java Validator2.java
// where docs is a subdirectory and *.java are in the current dir.

import java.util.*;
import java.text.*;

/******************************************************************************
 * The <code>Validator2</code> class is the validation portion of the payroll
 * application.
 * @author John Phillips
 * @version 1.0.0
 *****************************************************************************/
public class Validator2
{
    /***************************************************************************
     * Returns a <code>double</code> if valid. 
     * @param sc A <code>Scanner</code> that is used for input.
     * @param prompt A <code>String</code> that is used for a prompt.
     **************************************************************************/
    public static double getDouble(Scanner sc, String prompt)
    {
        double d = 0.0;
        boolean isValid = false;
        while (isValid == false)
        {
            System.out.print(prompt);
            if (sc.hasNextDouble())
            {
                d = sc.nextDouble();
                isValid = true;
            }
            else
            {
                System.out.println ("Error! Invalid decimal value. Try again.");
            }
            sc.nextLine();  // discard any other data entered on the line
        }
        return d;
    }

    /***************************************************************************
     * Returns a <code>double</code> if it is within range. 
     * @param sc A <code>Scanner</code> that is used for input.
     * @param prompt A <code>String</code> that is used to for a prompt.
     * @param min A <code>double</code> that is the mininum value the input can be.
     * @param max A <code>double</code> that is the maximum value the input can be.
     **************************************************************************/
    public static double getDoubleWithinRange(Scanner sc, String prompt, double min, double max)
    {
        double d = 0.0;
        boolean isValid = false;
        while (isValid == false)
        {
            d = getDouble(sc, prompt);
            if (d <= min)
            {
                System.out.println ("Error! Number must be greater than " + min + ".");
            }
            else if (d >= max)
            {
                System.out.println ("Error! Number must be less than " + max + ".");
            }
            else
            {
                isValid = true;
            }
        }
        return d;
    }

    /***************************************************************************
     * Returns a <code>int</code> if valid. 
     * @param sc A <code>Scanner</code> that is used to get input.
     * @param prompt A <code>String</code> that holds a prompt.
     **************************************************************************/
    public static int getInt(Scanner sc, String prompt)
    {
        int i = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            System.out.print(prompt);
            if (sc.hasNextInt())
            {
                i = sc.nextInt();
                isValid = true;
            }
            else
            {
                System.out.println ("Error! Invalid integer value. Try again.");
            }
            sc.nextLine();  // discard any other data entered on the line
        }
        return i;
    }

    /***************************************************************************
     * Returns a <code>int</code> if it is within range. 
     * @param sc A <code>Scanner</code> that is used to get input.
     * @param prompt A <code>String</code> that holds a prompt.
     * @param min An <code>int</code> that is the minimum the input can be.
     * @param max An <code>int</code> that is the maximum the input can be.
     **************************************************************************/
    public static int getIntWithinRange(Scanner sc, String prompt, int min, int max)
    {
        int i = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            i = getInt(sc, prompt);
            if (i <= min)
            {
                System.out.println ("Error! Number must be greater than " + min + ".");
            }
            else if (i >= max)
            {
                System.out.println ("Error! Number must be less than " + max + ".");
            }
            else
            {
                isValid = true;
            }
        }
        return i;
    }

    /***************************************************************************
     * Test of the validator2 class.
     * @param args[] A <code>String array</code> not used by this application.
     **************************************************************************/
    public static void main( String[] args)
    {
        double d;
        int i;
        Scanner sc = new Scanner(System.in);
        d = getDouble(sc, "Enter a double: ");
        System.out.println("Validated double is " + d);
        d = getDoubleWithinRange(sc, "Enter double between 1.5 and 2.75: ",1.5,2.75);
        System.out.println("Validated double within range is " + d); 
    } 
}
